//
//  PolicyFileService.m
//  homeland
//
//  Created by SunShine on 13-10-29.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "PolicyFileService.h"

@implementation PolicyFileService

-(void)loadDataWithKey:(NSString *)keyOrNil
{
    if (self.dataList) {
        self.dataList = nil;
    }
    _dataItemName = @"EnterprisePolicyFileItem";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"" forKey:@"unit_name"];
    if (self.unitName) {
        [params setValue:_unitName forKey:@"fromDept"];
    }
    if (keyOrNil) {
        [params setValue:keyOrNil forKey:@"title"];
    }
    [self doHttpPost:ENTERPRISE_POLICY_FILE_URL headers:nil path:nil parameters:params];
}

-(void)getFileContentForID:(NSString *)contentID
{
    [self doHttpGet:ENTERPRISE_POLICY_FILE_CONTENT_URL headers:nil path:[NSString stringWithFormat:@"%d",contentID.integerValue] parameters:nil];
}

-(void)doHttpGet:(NSString *)posturl headers:(NSDictionary *)headersOrNil path:(NSString *)path parameters:(NSDictionary *)params
{
    _currentEngine = [[MKNetworkEngine alloc] initWithHostName:posturl customHeaderFields:headersOrNil];
    MKNetworkOperation *op = [_currentEngine operationWithPath:path params:params httpMethod:@"GET"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        NSDictionary *json = [operation responseJSON];
        //转换对象,添加到列表中
        EnterprisePolicyFileContentItem *content = getObjectFromDic([EnterprisePolicyFileContentItem class], json);
        self.dataList = [NSArray arrayWithObject:content];
        SuccessBlock(json);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        FailBlock(err);
    }];
    [_currentEngine enqueueOperation:op];
}
@end
